﻿#Region "License & Usage"

'Microsoft Public License (Ms-PL) 

'This license governs use of the accompanying software. If you use the 
'software, you accept this license. If you do not accept the license, do 
'not use the software. 

'1. Definitions 

'The terms "reproduce," "reproduction," "derivative works," and 
'"distribution" have the same meaning here as under U.S. copyright law. 

'A "contribution" is the original software, or any additions or changes 
'to the software. 

'A "contributor" is any person that distributes its contribution under 
'this license. 

'"Licensed patents" are a contributor's patent claims that read directly 
'on its contribution. 

'2. Grant of Rights 

'(A) Copyright Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free copyright license to 
'reproduce its contribution, prepare derivative works of its 
'contribution, and distribute its contribution or any derivative works 
'that you create. 

'(B) Patent Grant- Subject to the terms of this license, including the 
'license conditions and limitations in section 3, each contributor grants 
'you a non-exclusive, worldwide, royalty-free license under its licensed 
'patents to make, have made, use, sell, offer for sale, import, and/or 
'otherwise dispose of its contribution in the software or derivative 
'works of the contribution in the software. 

'3. Conditions and Limitations 

'(A) No Trademark License- This license does not grant you rights to use 
'any contributors' name, logo, or trademarks. 

'(B) If you bring a patent claim against any contributor over patents 
'that you claim are infringed by the software, your patent license from 
'such contributor to the software ends automatically. 

'(C) If you distribute any portion of the software, you must retain all 
'copyright, patent, trademark, and attribution notices that are present 
'in the software. 

'(D) If you distribute any portion of the software in source code form, 
'you may do so only under this license by including a complete copy of 
'this license with your distribution. If you distribute any portion of 
'the software in compiled or object code form, you may only do so under a 
'license that complies with this license. 

'(E) The software is licensed "as-is." You bear the risk of using it. The 
'contributors give no express warranties, guarantees or conditions. You 
'may have additional consumer rights under your local laws which this 
'license cannot change. To the extent permitted under your local laws, 
'the contributors exclude the implied warranties of merchantability, 
'fitness for a particular purpose and non-infringement.

#End Region

#Region "Options"

Option Strict On
Option Explicit On

#End Region

#Region "Imports"

'From .Net Framework 3.5
Imports Microsoft.Drawing
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms

'From Custom Classes/Reference
Imports StedySoft.SenseSDK
Imports StedySoft.SenseSDK.DrawingCE
Imports SenseSDKExtended.DrawingCEEx

#End Region

Namespace SensePanelSelectionControl

    ''' <summary>SenseSelectionControl.SelectionStyle2</summary>
    ''' <author>Greenhacks</author>
    ''' <author-reference>None</author-reference>
    ''' <copyright>None</copyright>
    ''' <version>0.1</version>
    ''' <remarks>None</remarks>
    ''' <reference>None</reference>
    Public Class SelectionStyle2
        Inherits SensePanelBase
        Implements IDisposable

#Region "Fields"

        'General Flags
        Private _disposed As Boolean = False
        Private _buttonSelected As Boolean = False

        'General Properties
        Private _name As String = Nothing

        Private _imageEnabled As IImage = _getIImageFromResource("selection.gray")
        Private _imagePressed As IImage = _getIImageFromResource("selection.green")

        Private _months As ArrayList = New ArrayList

        Private _januarySelected As Boolean
        Private _februarySelected As Boolean
        Private _marchSelected As Boolean
        Private _aprilSelected As Boolean
        Private _maySelected As Boolean
        Private _juneSelected As Boolean
        Private _julySelected As Boolean
        Private _augustSelected As Boolean
        Private _septemberSelected As Boolean
        Private _octoberSelected As Boolean
        Private _novemberSelected As Boolean
        Private _decemberSelected As Boolean


#End Region

#Region "Methods"

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <remarks>None</remarks>
        Public Sub New()

            Try

                'Do Nothing

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

        ''' <summary>
        ''' Constructor
        ''' </summary>
        ''' <remarks>None</remarks>
        Public Sub New(ByVal Name As String)

            Try

                'Save name property
                _name = Name

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

        ''' <summary>
        ''' Dispose
        ''' </summary>
        ''' <remarks>None</remarks>
        Public Overrides Sub Dispose()

            Try

                Me._dispose(True)
                GC.SuppressFinalize(Me)
                MyBase.Dispose()

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Sub

        ''' <summary>
        ''' _Dispose
        ''' </summary>
        ''' <param name="disposing"></param>
        ''' <remarks>None</remarks>
        Private Sub _dispose(ByVal disposing As Boolean)

            Try

                Me._disposed = True

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try
        End Sub

        ''' <summary>
        ''' Check if it is VGA or QVGA
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _isVGA() As Boolean

            Try

                'Return the output
                Return StedySoft.SenseSDK.DrawingCE.Resolution.ScreenIsVGA

            Catch ex As Exception

                'Throw exception if any other error caught
                Throw New Exception(ex.Message)

            End Try

        End Function

        ''' <summary>
        ''' _getIImageFromResource
        ''' </summary>
        ''' <param name="resource"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function _getIImageFromResource(ByVal resource As String) As IImage
            Dim iimg As IImage = Nothing
            Using strm As MemoryStream = If(_isVGA, SenseSDKExtended.Resources.VGA.Images.GetIImageFromResource(resource), SenseSDKExtended.Resources.QVGA.Images.GetIImageFromResource(resource))
                ImagingFactory.GetImaging.CreateImageFromBuffer(strm.GetBuffer, Convert.ToUInt32(strm.Length), BufferDisposalFlag.BufferDisposalFlagNone, iimg)
            End Using
            Return iimg
        End Function

        <CLSCompliant(False)> _
        Public Structure MonthStructure

            Dim _monthButtonRectangle As Rectangle
            Dim _monthButtonAbbreviate As String
            Dim _monthButtonSelected As Boolean

        End Structure

        Public Event OnClickMonths(ByVal sender As Object, ByVal value As String)

        Public Overrides Sub OnMouseDown(ByVal x As Integer, ByVal y As Integer, ByRef cancelScroll As Boolean)
            MyBase.OnMouseDown(x, y, cancelScroll)

        End Sub

        Public Overrides Sub OnMouseUp(ByVal x As Integer, ByVal y As Integer)
            MyBase.OnMouseUp(x, y)

            Dim mousePoint As System.Drawing.Point = System.Windows.Forms.Control.MousePosition()
            Dim pointX As Integer = mousePoint.X - MyBase.ClientRectangle.X
            Dim pointY As Integer = (mousePoint.Y - (Screen.PrimaryScreen.Bounds.Height - DrawingCE.Resolution.ScreenHeight)) - ClientRectangle.Top
            Dim _selectedMonths As String = ","

            For i As Integer = 0 To 11

                Select Case pointX

                    Case DirectCast(DirectCast(_months.Item(i), System.Object), SelectionStyle2.MonthStructure)._monthButtonRectangle.Left To DirectCast(DirectCast(_months.Item(i), System.Object), SelectionStyle2.MonthStructure)._monthButtonRectangle.Left + DirectCast(DirectCast(_months.Item(i), System.Object), SelectionStyle2.MonthStructure)._monthButtonRectangle.Width

                        Select Case pointY

                            Case DirectCast(DirectCast(_months.Item(i), System.Object), SelectionStyle2.MonthStructure)._monthButtonRectangle.Top - ClientRectangle.Top To (DirectCast(DirectCast(_months.Item(i), System.Object), SelectionStyle2.MonthStructure)._monthButtonRectangle.Top - ClientRectangle.Top) + DirectCast(DirectCast(_months.Item(i), System.Object), SelectionStyle2.MonthStructure)._monthButtonRectangle.Height

                                Select Case DirectCast(DirectCast(_months.Item(i), System.Object), SelectionStyle2.MonthStructure)._monthButtonAbbreviate

                                    Case "Jan"
                                        If _januarySelected Then _januarySelected = False Else _januarySelected = True

                                    Case "Feb"
                                        If _februarySelected Then _februarySelected = False Else _februarySelected = True

                                    Case "Mar"
                                        If _marchSelected Then _marchSelected = False Else _marchSelected = True

                                    Case "Apr"
                                        If _aprilSelected Then _aprilSelected = False Else _aprilSelected = True

                                    Case "May"
                                        If _maySelected Then _maySelected = False Else _maySelected = True

                                    Case "Jun"
                                        If _juneSelected Then _juneSelected = False Else _juneSelected = True

                                    Case "Jul"
                                        If _julySelected Then _julySelected = False Else _julySelected = True

                                    Case "Aug"
                                        If _augustSelected Then _augustSelected = False Else _augustSelected = True

                                    Case "Sep"
                                        If _septemberSelected Then _septemberSelected = False Else _septemberSelected = True

                                    Case "Oct"
                                        If _octoberSelected Then _octoberSelected = False Else _octoberSelected = True

                                    Case "Nov"
                                        If _novemberSelected Then _novemberSelected = False Else _novemberSelected = True

                                    Case "Dec"
                                        If _decemberSelected Then _decemberSelected = False Else _decemberSelected = True

                                End Select

                        End Select

                End Select

            Next

            If _januarySelected Then _selectedMonths &= "Jan,"
            If _februarySelected Then _selectedMonths &= "Feb,"
            If _marchSelected Then _selectedMonths &= "Mar,"
            If _aprilSelected Then _selectedMonths &= "Apr,"
            If _maySelected Then _selectedMonths &= "May,"
            If _juneSelected Then _selectedMonths &= "Jun,"
            If _julySelected Then _selectedMonths &= "Jul,"
            If _augustSelected Then _selectedMonths &= "Aug,"
            If _septemberSelected Then _selectedMonths &= "Sep,"
            If _octoberSelected Then _selectedMonths &= "Oct,"
            If _novemberSelected Then _selectedMonths &= "Nov,"
            If _decemberSelected Then _selectedMonths &= "Dec,"

            _selectedMonths = _selectedMonths.TrimStart(CChar(",")).TrimEnd(CChar(","))

            RaiseEvent OnClickMonths(Me, _selectedMonths)

        End Sub

        ''' <summary>
        ''' OnRender
        ''' </summary>
        ''' <param name="g"></param>
        ''' <remarks></remarks>
        Public Overrides Sub OnRender(ByVal g As System.Drawing.Graphics)
            MyBase.OnRender(g)

            Dim _leftStart As Integer = CInt((Screen.PrimaryScreen.Bounds.Width - If(_isVGA, 432, 216)) / 2)
            Dim _buttonWidth As Integer = If(_isVGA, 36, 18)
            Dim _buttonCount As Integer = 12
            Dim _monthAbbreviate As String = Nothing
            Dim _monthAbbreviateSingle As String = Nothing
            Dim _imageButton As IImage = Nothing
            Dim _monthDetails As MonthStructure = New MonthStructure

            _months.Clear()

            For Count As Integer = 0 To _buttonCount - 1

                _monthAbbreviate = MonthName(Count + 1, True)

                Select Case _monthAbbreviate

                    Case "Jan"
                        _monthDetails._monthButtonSelected = January
                    Case "Feb"
                        _monthDetails._monthButtonSelected = February
                    Case "Mar"
                        _monthDetails._monthButtonSelected = March
                    Case "Apr"
                        _monthDetails._monthButtonSelected = April
                    Case "May"
                        _monthDetails._monthButtonSelected = May
                    Case "Jun"
                        _monthDetails._monthButtonSelected = June
                    Case "Jul"
                        _monthDetails._monthButtonSelected = July
                    Case "Aug"
                        _monthDetails._monthButtonSelected = August
                    Case "Sep"
                        _monthDetails._monthButtonSelected = September
                    Case "Oct"
                        _monthDetails._monthButtonSelected = October
                    Case "Nov"
                        _monthDetails._monthButtonSelected = November
                    Case "Dec"
                        _monthDetails._monthButtonSelected = December

                End Select

                _monthAbbreviateSingle = _monthAbbreviate.Substring(0, 1)

                _imageButton = If(_monthDetails._monthButtonSelected, _imagePressed, _imageEnabled)

                g.DrawImageAlphaChannel(_imageButton, New Rectangle(_leftStart + (_buttonWidth * Count), ClientRectangle.Top + CInt((ClientRectangle.Height - If(_isVGA, 36, 18)) / 2), If(_isVGA, 36, 18), If(_isVGA, 36, 18)))

                Using f As Font = New Font("Helvetica Neue OTS", 9.0F, FontStyle.Regular)
                    Dim s As String = _monthAbbreviateSingle
                    Dim r As Rectangle = New Rectangle(_leftStart + (_buttonWidth * Count), ClientRectangle.Top + CInt((ClientRectangle.Height - If(_isVGA, 36, 18)) / 2), If(_isVGA, 36, 18), If(_isVGA, 36, 18))
                    Using b As Brush = New SolidBrush(If(_monthDetails._monthButtonSelected, Color.White, Color.Gray))
                        Using sf As StringFormat = New StringFormat()
                            sf.Alignment = StringAlignment.Center
                            sf.LineAlignment = StringAlignment.Center
                            g.DrawString(s, f, b, r, sf)
                        End Using
                    End Using
                End Using


                _monthDetails._monthButtonAbbreviate = _monthAbbreviate
                _monthDetails._monthButtonRectangle = New Rectangle(_leftStart + (_buttonWidth * Count), ClientRectangle.Top + CInt((ClientRectangle.Height - If(_isVGA, 36, 18)) / 2), If(_isVGA, 36, 18), If(_isVGA, 36, 18))

                _months.Add(_monthDetails)

            Next

        End Sub

#End Region

#Region "Properties"

        Public Property January() As Boolean
            Get
                Return _januarySelected
            End Get
            Set(ByVal value As Boolean)
                _januarySelected = value
            End Set
        End Property

        Public Property February() As Boolean
            Get
                Return _februarySelected
            End Get
            Set(ByVal value As Boolean)
                _februarySelected = value
            End Set
        End Property

        Public Property March() As Boolean
            Get
                Return _marchSelected
            End Get
            Set(ByVal value As Boolean)
                _marchSelected = value
            End Set
        End Property

        Public Property April() As Boolean
            Get
                Return _aprilSelected
            End Get
            Set(ByVal value As Boolean)
                _aprilSelected = value
            End Set
        End Property

        Public Property May() As Boolean
            Get
                Return _maySelected
            End Get
            Set(ByVal value As Boolean)
                _maySelected = value
            End Set
        End Property

        Public Property June() As Boolean
            Get
                Return _juneSelected
            End Get
            Set(ByVal value As Boolean)
                _juneSelected = value
            End Set
        End Property

        Public Property July() As Boolean
            Get
                Return _julySelected
            End Get
            Set(ByVal value As Boolean)
                _julySelected = value
            End Set
        End Property

        Public Property August() As Boolean
            Get
                Return _augustSelected
            End Get
            Set(ByVal value As Boolean)
                _augustSelected = value
            End Set
        End Property

        Public Property September() As Boolean
            Get
                Return _septemberSelected
            End Get
            Set(ByVal value As Boolean)
                _septemberSelected = value
            End Set
        End Property

        Public Property October() As Boolean
            Get
                Return _octoberSelected
            End Get
            Set(ByVal value As Boolean)
                _octoberSelected = value
            End Set
        End Property

        Public Property November() As Boolean
            Get
                Return _novemberSelected
            End Get
            Set(ByVal value As Boolean)
                _novemberSelected = value
            End Set
        End Property

        Public Property December() As Boolean
            Get
                Return _decemberSelected
            End Get
            Set(ByVal value As Boolean)
                _decemberSelected = value
            End Set
        End Property

#End Region

    End Class

End Namespace